import React, { Suspense } from "react";
import { withRouter } from "react-router-dom";
import { connect } from "react-redux";
import action from "../../services/action.js";
import { getRouterRequest } from "@router/store/action";
import { isloginRequest } from "../../pages/App/store/action";
import WebSocket from "../Websocket/websocket";
import Loading from "../Loading/index";
var judge;
class CheckLogin extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      setjudge: false,
    };
  }
  componentDidMount() {
    // const obj = { method: "loginRequest", api: "web/rest/user/isLogin", cmd: "isLogin", request: {} };
    this.props.isLoginRequest();
    if (this.props.isGetMenu) {
      this.props.getMenu(this.props.history);
    }
    if (!this.props.isLogin) {
      this.props.history.push("/login");
    }
    var judge = this.browserRedirect();
    if (judge) {
      if (localStorage.getItem("role") === "admin") {
        this.setState({
          setjudge: true,
        });
        window.location.href = "/Ipadkanban";
      } else {
        this.props.history.push("/IpadLineinspector");
      }
    }
  }

  componentDidUpdate() {
    if (!this.props.isLogin) {
      this.props.history.push("/login");
    }
  }
  browserRedirect() {
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/ipad/i) === "ipad";
    var bIsAndroid = sUserAgent.match(/android/i) === "android";
    var bIsIphone = sUserAgent.match(/iphone os/i) === "iphone os";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) === "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) === "web";
    var bIsCE = sUserAgent.match(/windows ce/i) === "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) === "windows mobile";
    if (
      bIsIpad ||
      bIsAndroid ||
      bIsIphone ||
      // bIsMidp ||
      bIsUc7 ||
      bIsUc ||
      bIsCE ||
      bIsWM
    ) {
      return true;
    }
    return false;
  }
  render() {
    return (
      <>
        {this.state.setjudge ? <Loading /> : <></>}
        {/* <WebSocket /> */}
        {this.props.children}
      </>
    );
  }
}
const mapStatetoProps = (state) => ({
  isLogin: state.login.isLogin,
  systemRole: state.login.systemRole,
});
const mapDispatchToProps = (dispatch) => {
  return {
    getMenu() {
      dispatch(getRouterRequest(true));
    },
    isLoginRequest(obj) {
      dispatch(isloginRequest(obj));
    },
  };
};
export default connect(
  mapStatetoProps,
  mapDispatchToProps
)(withRouter(CheckLogin));
